﻿using System;
using System.Xml;

/// <summary>
/// Summary description for RssHelper
/// </summary>
/// 
namespace QzEbook.Common
{
    public class RssHelper
    {
        public RssHelper()
        {
        }
        /// <summary>
        /// 写入头部信息;
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="title"></param>
        /// <param name="link"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public static XmlTextWriter WriteRSSPrologue(XmlTextWriter writer,string title,string link,string description)
        {
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteRaw("<?xml-stylesheet href=\"../skins/rss2.xsl\" type=\"text/xsl\"?>");
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            writer.WriteAttributeString("xmlns:blogChannel", "http://www.newpub.cn/ebookChannel");
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", title);
            writer.WriteElementString("link", link);
            writer.WriteElementString("description", description);
            writer.WriteElementString("copyright", "Copyright 2007-2008 crlnet");
            writer.WriteElementString("generator", "qzEbook rss .o1");

            return writer;
        }


        /// <summary>
        /// 写入项;
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="Title"></param>
        /// <param name="Link"></param>
        /// <param name="Category"></param>
        /// <param name="Description"></param>
        /// <param name="Date"></param>
        /// <returns></returns>
        public static XmlTextWriter AddRSSItem(XmlTextWriter writer, string Title, string Link, string Category, string Description, DateTime Date)
        {
            writer.WriteStartElement("item");
            //写入标题;
            writer.WriteStartElement("title");
            writer.WriteCData(Title);
            writer.WriteEndElement();

            //写入导向路径;
            writer.WriteElementString("link", Link);
            //写入分类;
            writer.WriteStartElement("category");
            writer.WriteCData(Category);
            writer.WriteEndElement();
            //写入作者
            writer.WriteElementString("author", "zzmmcrl@163.com(crl)");
            //写入定义;
            writer.WriteStartElement("description");
            writer.WriteCData(Description);
            writer.WriteEndElement();

            string DateString = string.Format("{0:R}", Date).Replace("GMT", "+0800");

            writer.WriteElementString("pubDate", DateString);
            writer.WriteEndElement();

            return writer;
        }

        /// <summary>
        /// 关闭文档流;
        /// </summary>
        /// <param name="writer"></param>
        /// <returns></returns>
        public static XmlTextWriter WriteRSSClosing(XmlTextWriter writer)
        {
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();

            return writer;
        }

    }
}